#include <lpc23xx.h>
#include "LCD.H"
void FIQ_Handler (void)__irq; 
void HardwareInit (void);
unsigned int flasher;
unsigned int delay;	

int main (void)
{
			
HardwareInit ();
   SCS             = 0x00000020;       //enable the external oscillator
while (!( SCS &0x00000040))         //Wait for it to settle
{
;
}
    
CLKSRCSEL       = 0x00000001;       //Select the external oscillator as the main oscillator

//configure pll
PLLCFG          = 0x00000005;       //Set the PLL M and N constants
PLLFEED         = 0x000000AA;       //Write the feed sequence
PLLFEED         = 0x00000055;
//spin up pll
PLLCON          = 0x00000001;       //Enable the PLL
PLLFEED         = 0x000000AA;       //Write the feed sequence
PLLFEED         = 0x00000055;

while(!( PLLSTAT & 0x04000000))     //Wait until the PLL locks
{
;
}

//set clock dividers
CCLKCFG         = 0x00000001;       //Set the CPU divider

//connect pll
PLLCON          |= 0x00000002;      //Connect the PLL
PLLFEED         = 0x000000AA;       //Write the feed sequence
PLLFEED         = 0x00000055;
    lcd_init();					   //
	lcd_clear();				  //
	set_cursor(2,0);			   // Agaf=/
    lcd_print("PLL Example");	 //
while(1)
{

FIO2CLR         = 0x000000FF;

for(flasher =1;flasher<0x00000100;flasher = flasher <<1)
    {
       FIO2SET = flasher; 
        for(delay = 0;delay<0x30000;delay++)				  	//simple delay loop
        {
          ;
        }
    }
  FIO2CLR = 0x000000FF;
}  
    
 

return(0);
}

void FIQ_Handler (void) __irq
{
  
unsigned char debounce; 

PLLCON          ^= 0x00000002;      //disconnect the PLL
PLLFEED         = 0x000000AA;       //Write the feed sequence
PLLFEED         = 0x00000055;

for(debounce = 0;debounce<0xFE;debounce++) //Debounce the switch
  {
    ;
  }


EXTINT  		= 0x00000001;			    //Clear the peripheral interrupt flag
}

void HardwareInit (void)
{
PINSEL0         = 0x00000000;
PINSEL1         = 0x00000000;
PINSEL2         = 0x00000000;
PINSEL3         = 0x00000000;
PINSEL4         = 0x00000000;
PINSEL5         = 0x00000000;
PINSEL6         = 0x00000000;
PINSEL7         = 0x00000000;
PINSEL8         = 0x00000000;
PINSEL9         = 0x00000000;
PINSEL10        = 0x00000000;
    
IODIR0          = 0x00000000;
IODIR1          = 0x00000000;
IOSET0          = 0x00000000;
IOSET1          = 0x00000000;
   
FIO0DIR         = 0x00000000;
FIO1DIR         = 0x00000000;
FIO2DIR         = 0x00000000;
FIO3DIR         = 0x00000000;
FIO4DIR         = 0x00000000;
    
FIO0SET         = 0x00000000;
FIO1SET         = 0x00000000;
FIO2SET         = 0x00000000;
FIO3SET         = 0x00000000;
FIO4SET         = 0x00000000;					

PINSEL4 	    = 0x00100000;						//Enable the EXTINT1 interrupt
EXTMODE         = 0x00000001;                       //Set INT0 to edge sensetive mode
EXTPOLAR        = 0x00000001;                       //Select rising edge
EXTINT          = 0x00000001;                       //Clear the interrupt flags
VICIntSelect    = 0x00004000;
VICIntEnable    = 0x00004000;						//enable interrupt
FIO2DIR         = 0x000000FF;   
FIO2MASK        = 0xFFFFFF00;
    
}
